from django.db import models

# Create your models here.
from utils.modelsMiXin import ModelSetMiXin


class Classification(ModelSetMiXin):
    name = models.CharField(max_length=40,verbose_name='分类名称')
    parent = models.ForeignKey('self',on_delete=models.SET_NULL,null=True,blank=True)

    class Meta:
        db_table = 'classification'
        verbose_name = '商品分类表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.id}.{self.name}'

class Commodity(ModelSetMiXin):
    STATUS_CHOICES = (
        (0,'未发售'),
        (1,'已发售')
    )

    name = models.CharField(max_length=40,verbose_name='商品名')
    caption = models.CharField(max_length=150,verbose_name='副标题')
    brand = models.CharField(max_length=40,verbose_name='品牌',null=True,blank=True)
    price = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='价格') #digits最多多少位，decimal是小数点后几位
    stock = models.IntegerField(verbose_name='库存')
    serviceafter_sale = models.TextField(verbose_name='售后服务',null=True,blank=True)
    pack = models.TextField(verbose_name='包装信息')
    sales = models.IntegerField(verbose_name='销售量',default=0)
    comments = models.IntegerField(verbose_name='评价数量',default=0)
    status = models.IntegerField(default=0,verbose_name='状态',choices=STATUS_CHOICES)
    details = models.TextField(verbose_name='商品详情',null=True,blank=True)

    classification1 = models.ForeignKey(Classification,on_delete=models.PROTECT,verbose_name='一级分类',related_name='classification1') #PROTECT当外键有数据在用时，主键不可以直接删除；
    classification2 = models.ForeignKey(Classification,on_delete=models.PROTECT,verbose_name='二级分类',related_name='classification2') #PROTECT当外键有数据在用时，主键不可以直接删除；

    class Meta:
        ordering = ['-sales','-comments','-create_time']
        db_table = 'commodity'
        verbose_name = '商品'
        verbose_name_plural = verbose_name

class CommodityImg(models.Model):
    src = models.TextField(verbose_name='图片地址')
    priority = models.IntegerField(default=0,verbose_name='优先级')
    commodity = models.ForeignKey(Commodity,on_delete=models.CASCADE,verbose_name='商品')

    class Meta:
        ordering = ['-priority','-id']
        db_table = 'commodity_img'
        verbose_name = '商品图'
        verbose_name_plural = verbose_name





